﻿/*
Common.js
 
*/

function getx(id, tagName) {
    var ele = document.getElementById(id);
    if (!tagName)
        return ele;
    else
        return ele.getElementsByTagName(tagName);
}

//
// Note: onRowStatusChanged can be specified for callback
//
function checkAll(chk) {
    var tblBody = chk.parentNode.parentNode.parentNode;
    for (var i = 1; i < tblBody.children.length; i++) {
        var tr = tblBody.children[i];
        var firstTd = tr.children[0];

        for (var j = 0; j < firstTd.children.length; j++) {
            if (firstTd.children[j] && firstTd.children[j].type == "checkbox") {
                if (firstTd.children[j].checked != chk.checked) {
                    firstTd.children[j].checked = chk.checked;
                    if (onRowStatusChanged) {
                        onRowStatusChanged(firstTd.children[j], i);
                    }
                }
                break;
            }
        }
    }
}

// returns count of checked items
function getCheckedItemsCount(gv) {
    var count = 0;
    var table = document.getElementById(gv);
    var tblBody = table.children[0];
    for (var i = 1; i < tblBody.children.length; i++) {
        var tr = tblBody.children[i];
        var firstTd = tr.children[0];

        for (var j = 0; j < firstTd.children.length; j++) {
            if (firstTd.children[j] && firstTd.children[j].type == "checkbox") {
                if (firstTd.children[j].checked) {
                    count++;
                }
                break;
            }
        }
    }
    return count;
}

/**********************************************************
WaterMarkTextBox: 给TextBox加上默认的水印文字
兼容 firefox 和 ie 7/8, ie6没有测试 
作者：黑米
时间：2020-1-1
**********************************************************/
WaterMarkTextBox = function(textbox, defaultText) {
    this.textbox = textbox;
    this.defaultText = defaultText;
    this.actualValue = "";

    // set default status
    this.onTextBoxLostFocus();
}

WaterMarkTextBox.prototype.init = function() {
    if (!this.textbox) return;
    var oThis = this;
    if (window.addEventListener) {
        this.textbox.addEventListener("focus", function() { oThis.onTextBoxFocus(); }, false);
        this.textbox.addEventListener("blur", function() { oThis.onTextBoxLostFocus(); }, false);
    }
    else if (window.attachEvent) {
        this.textbox.attachEvent("onfocus", function() { oThis.onTextBoxFocus(); });
        this.textbox.attachEvent("onblur", function() { oThis.onTextBoxLostFocus(); });
    }
}

WaterMarkTextBox.prototype.onTextBoxFocus = function() {
    if (!this.actualValue || this.actualValue == "") {
        this.textbox.value = "";
        this.textbox.style.color = "#000000";
    }
}

WaterMarkTextBox.prototype.onTextBoxLostFocus = function() {
    this.actualValue = this.textbox.value;
    if (!this.actualValue || this.actualValue == "") {
        this.textbox.value = this.defaultText;
        this.textbox.style.color = "#888888";
    }
}



// returns a + b, ignore duplicated item
function appendClass(srcClassName, className) {

    if (typeof className == "undefined") return srcClassName;
    if (!srcClassName) return className;

    var classes = srcClassName.split(/\s+/);
    for (var i = 0; i < classes.length; i++) {
        if (classes[i] == className) return srcClassName;
    }
    classes[classes.length] = className;
    return classes.join(" ");
}

// returns a - b
function removeClass(srcClassName, className) {
    if (!className || !srcClassName) return srcClassName;
    var classes = srcClassName.split(/\s+/);
    for (var i = 0; i < classes.length; i++) {
        if (classes[i] == className) {
            classes[i] = null;
            break;
        }
    }
    return classes.join(" ");
}

// find the element by given id and click it
function getElementAndClick(id) {
    var e = document.getElementById(id);
    if (e) {
        clickElement(e);
    }
}

// click the element
function clickElement(element) {
    if (element) {
        if (element.click) element.click();
        else if (element.onclick) element.onclick();
    }
}

// find children elements by tag and set the enabled status
function enableChildren(enabled, element, tagName) {
    var nodes = null;
    if (!element.hasChildNodes()) return;
    if (tagName)
        nodes = element.getElementsByTagName(tagName);
    else
        nodes = document.body.childNodes;
    if (nodes) {
        for (var i = 0; i < nodes.length; i++) {
            nodes[i].disabled = !enabled;
        }
    }
}

// change visible of the element
function toggleVisibleByID(id) {
    return toggleVisible(getx(id));
}

// change visible of the element
function toggleVisible(element) {
    if (element) {
        if (!element.style.display || element.style.display == "block") {
            element.style.display = "none";
            return true;
        }
        else {
            element.style.display = "block";
            return false;
        }
    }
    else return false;
}
